// pages/pay/pay.js
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		orderId: "",
		type: "",
		payType: "",
		h5Token: "",
		businessTypeCode: "",
		extraPayId: "",
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		console.log(options, "options")
		const { type, orderId, payType, h5token, businessTypeCode, extraPayId } =
			options
		this.setData({
			orderId,
			type,
			payType,
			h5Token: h5token,
			businessTypeCode,
			extraPayId,
		})
		if (type === "orderId") {
			this.orderPay()
		} else if (type === "code") {
			// 文件代办支付
			this.orderPay()
		} else if (type === "fileExtra") {
			this.fileExtraPay()
		} else if (type === "guide") {
			console.log("引流页面跳转 支付")
			this.guidePay()
		} else if (type === "production") {
			// 商城支付
			this.productionPay()
		} else if (type === "translate") {
			// 文件翻译支付
			this.translatePay()
		} else if (type === "studyAbroad") {
			// 出国留学支付
			this.studyAbroadPay()
		} else if (type === "newFileAgency") {
			// 新文件代办支付
			this.newFileAgencyPay()
		}
	},
	//  文件代办尾款支付
	fileExtraPay() {
		wx.login({
			success: (code) => {
				const data = {
					code: code.code,
					payWay: 2,
					extraPayId: this.data.extraPayId,
				}
				wx.$API["person/fileExtraPay"](data)
					.then((res) => {
						console.log(res, "拿支付的参数")
						const { nonceStr, paySign, signType, timeStamp } = res.data[0]
						wx.requestPayment({
							timeStamp,
							nonceStr,
							package: res.data[0].package,
							signType,
							paySign,
							success: (res) => {
								wx.navigateBack({
									delta: 1,
								})
								console.log(res, "支付成功,跳回去")
							},
							fail: (err) => {
								console.log(err, "支付失败")
								if (err.errMsg === "requestPayment:fail cancel") {
									console.log("用户取消了支付")
								} else {
									console.log("支付出现了问题")
								}
								wx.navigateBack({
									delta: 1,
								})
							},
						})
					})
					.catch((error) => {
						console.log(error, "error")
						wx.showModal({
							title: "提示",
							content: error,
							showCancel: false,
							success(res) {
								wx.navigateBack({
									delta: 1,
								})
							},
						})
					})
				console.log(code, "拿到code")
			},
			fail: (error) => {
				console.log(error, "error")
				wx.showModal({
					title: "错误",
					content: "获取code失败!请联系相关管理员",
					success: (res) => {
						if (res.confirm) {
							console.log("用户点击确定")
						} else if (res.cancel) {
							console.log("用户点击取消")
						}
					},
				})
			},
		})
	},
	// 新文件代办支付
	newFileAgencyPay() {
		console.log("走到了这里")
		wx.login({
			success: (code) => {
				const data = {
					code: code.code,
					businessCode: this.data.orderId,
					tradeType: "JSAPI",
					isForTest: true,
					businessTypeCode: this.data.businessTypeCode,
				}
				const token = wx.getStorageSync("token")
				wx.$API["person/newFileAgencyPay"](data, {
					header: {
						// 'Goods-Token': this.data.h5Token,
						"X-Authorization": token,
					},
				})
					.then((res) => {
						console.log(res, "拿支付的参数")
						const { nonceStr, paySign, signType, timeStamp } = res.data
						wx.requestPayment({
							timeStamp,
							nonceStr,
							package: res.data.myPackage,
							signType,
							paySign,
							success: (res) => {
								wx.navigateBack({
									delta: 1,
								})
								console.log(res, "支付成功,跳回去")
							},
							fail: (err) => {
								console.log(err, "支付失败")
								if (err.errMsg === "requestPayment:fail cancel") {
									console.log("用户取消了支付")
									wx.navigateBack({
										delta: 1,
									})
								} else {
									console.log("支付出现了问题")
									wx.showModal({
										title: "温馨提示",
										content: JSON.stringify(err),
										showCancel: false,
									}).then(() => {
										wx.navigateBack({
											delta: 1,
										})
									})
								}
							},
						})
					})
					.catch((error) => {
						console.log(error, "error")
						// wx.showToast({
						//   title: error,
						//   icon: 'none',
						//   duration: 3000,
						// })
						wx.showModal({
							title: "温馨提示",
							content: error,
							showCancel: false,
						}).then(() => {
							wx.navigateBack({
								delta: 1,
							})
						})
					})
			},
			fail: (error) => {
				console.log(error, "error")
				wx.showModal({
					title: "错误",
					content: "获取code失败!请联系相关管理员",
					success: (res) => {
						wx.navigateBack({
							delta: 1,
						})
						if (res.confirm) {
							console.log("用户点击确定")
						} else if (res.cancel) {
							console.log("用户点击取消")
						}
					},
				})
			},
		})
	},
	//  出国留学项目支付
	studyAbroadPay() {
		console.log("走到了这里")
		wx.login({
			success: (code) => {
				const data = {
					code: code.code,
					orderId: this.data.orderId,
					payWay: 2,
				}
				const token = wx.getStorageSync("token")
				wx.$API["person/enrollPay"](data, {
					header: {
						// 'Goods-Token': this.data.h5Token,
						"X-Authorization": token,
					},
				})
					.then((res) => {
						console.log(res, "拿支付的参数")
						const { nonceStr, paySign, signType, timeStamp } = res.data
						wx.requestPayment({
							timeStamp,
							nonceStr,
							package: res.data.package,
							signType,
							paySign,
							success: (res) => {
								wx.navigateBack({
									delta: 1,
								})
								console.log(res, "支付成功,跳回去")
							},
							fail: (err) => {
								console.log(err, "支付失败")
								if (err.errMsg === "requestPayment:fail cancel") {
									console.log("用户取消了支付")
								} else {
									console.log("支付出现了问题")
								}
								wx.navigateBack({
									delta: 1,
								})
							},
						})
					})
					.catch((error) => {
						console.log(error, "error")
						wx.showToast({
							title: error,
							icon: "none",
							duration: 3000,
						})
						wx.navigateBack({
							delta: 1,
						})
					})
			},
			fail: (error) => {
				console.log(error, "error")
				wx.showModal({
					title: "错误",
					content: "获取code失败!请联系相关管理员",
					success: (res) => {
						wx.navigateBack({
							delta: 1,
						})
						if (res.confirm) {
							console.log("用户点击确定")
						} else if (res.cancel) {
							console.log("用户点击取消")
						}
					},
				})
			},
		})
	},
	productionPay() {
		wx.login({
			success: (code) => {
				const data = {
					code: code.code,
					id: this.data.orderId,
					payWay: 2,
				}
				const token = wx.getStorageSync("token")
				wx.$API["person/productionPay"](data, {
					header: {
						"Goods-Token": this.data.h5Token,
						"X-Authorization": token,
					},
				})
					.then((res) => {
						console.log(res, "拿支付的参数")
						const { nonceStr, paySign, signType, timeStamp } = res.data[0]
						wx.requestPayment({
							timeStamp,
							nonceStr,
							package: res.data[0].package,
							signType,
							paySign,
							success: (res) => {
								wx.navigateBack({
									delta: 1,
								})
								console.log(res, "支付成功,跳回去")
							},
							fail: (err) => {
								console.log(err, "支付失败")
								if (err.errMsg === "requestPayment:fail cancel") {
									console.log("用户取消了支付")
								} else {
									console.log("支付出现了问题")
								}
								wx.navigateBack({
									delta: 1,
								})
							},
						})
					})
					.catch((error) => {
						console.log(error, "error")
						wx.showToast({
							title: error,
							icon: "none",
							duration: 3000,
						})
						wx.navigateBack({
							delta: 1,
						})
					})
			},
			fail: (error) => {
				console.log(error, "error")
				wx.showModal({
					title: "错误",
					content: "获取code失败!请联系相关管理员",
					success: (res) => {
						if (res.confirm) {
							console.log("用户点击确定")
						} else if (res.cancel) {
							console.log("用户点击取消")
						}
					},
				})
			},
		})
	},
	translatePay() {
		wx.login({
			success: (code) => {
				const data = {
					code: code.code,
					orderId: this.data.orderId,
					payWay: 2,
					payType: this.data.payType,
				}
				wx.$API["login/translateWechatPay"](data)
					.then((res) => {
						console.log(res, "拿支付的参数")
						const { nonceStr, paySign, signType, timeStamp } = res.data[0]
						wx.requestPayment({
							timeStamp,
							nonceStr,
							package: res.data[0].package,
							signType,
							paySign,
							success: (res) => {
								wx.navigateBack({
									delta: 1,
								})
								console.log(res, "支付成功,跳回去")
							},
							fail: (err) => {
								console.log(err, "支付失败")
								if (err.errMsg === "requestPayment:fail cancel") {
									console.log("用户取消了支付")
								} else {
									console.log("支付出现了问题")
								}
								wx.navigateBack({
									delta: 1,
								})
							},
						})
					})
					.catch((error) => {
						console.log(error, "error")
						wx.showToast({
							title: error,
							icon: "none",
							duration: 3000,
						})
						wx.navigateBack({
							delta: 1,
						})
					})
				console.log(code, "拿到code")
			},
			fail: (error) => {
				console.log(error, "error")
				wx.showModal({
					title: "错误",
					content: "获取code失败!请联系相关管理员",
					success: (res) => {
						if (res.confirm) {
							console.log("用户点击确定")
						} else if (res.cancel) {
							console.log("用户点击取消")
						}
					},
				})
			},
		})
	},
	guidePay() {
		wx.login({
			success: (code) => {
				const data = {
					code: code.code,
					orderId: this.data.orderId,
					payWay: 1,
				}
				wx.$API["person/advertisingPay"](data)
					.then((res) => {
						console.log(res, "拿支付的参数")
						const { nonceStr, paySign, signType, timeStamp } = res.data
						wx.requestPayment({
							timeStamp,
							nonceStr,
							package: res.data.package,
							signType,
							paySign,
							success: (res) => {
								wx.navigateBack({
									delta: 1,
								})
								console.log(res, "支付成功,跳回去")
							},
							fail: (err) => {
								console.log(err, "支付失败")
								if (err.errMsg === "requestPayment:fail cancel") {
									console.log("用户取消了支付")
								} else {
									console.log("支付出现了问题")
								}
								wx.navigateBack({
									delta: 1,
								})
							},
						})
					})
					.catch((error) => {
						console.log(error, "error")
						wx.showToast({
							title: error,
							icon: "none",
							duration: 3000,
						})
						wx.navigateBack({
							delta: 1,
						})
					})
			},
			fail: (error) => {
				console.log(error, "error")
				wx.showModal({
					title: "错误",
					content: "获取code失败!请联系相关管理员",
					success: (res) => {},
				})
			},
		})
	},

	orderPay() {
		wx.login({
			success: (code) => {
				const data = {
					code: code.code,
					orderId: this.data.orderId,
					payWay: 2,
					payType: this.data.payType,
				}
				wx.$API["login/agencyWechatPay"](data)
					.then((res) => {
						console.log(res, "拿支付的参数")
						const { nonceStr, paySign, signType, timeStamp } = res.data[0]
						wx.requestPayment({
							timeStamp,
							nonceStr,
							package: res.data[0].package,
							signType,
							paySign,
							success: (res) => {
								wx.navigateBack({
									delta: 1,
								})
								console.log(res, "支付成功,跳回去")
							},
							fail: (err) => {
								console.log(err, "支付失败")
								if (err.errMsg === "requestPayment:fail cancel") {
									console.log("用户取消了支付")
								} else {
									console.log("支付出现了问题")
								}
								wx.navigateBack({
									delta: 1,
								})
							},
						})
					})
					.catch((error) => {
						console.log(error, "error")
						wx.showToast({
							title: error,
							icon: "none",
							duration: 3000,
						})
						wx.navigateBack({
							delta: 1,
						})
					})
				console.log(code, "拿到code")
			},
			fail: (error) => {
				console.log(error, "error")
				wx.showModal({
					title: "错误",
					content: "获取code失败!请联系相关管理员",
					success: (res) => {
						if (res.confirm) {
							console.log("用户点击确定")
						} else if (res.cancel) {
							console.log("用户点击取消")
						}
					},
				})
			},
		})
	},
})
